{{extend '../_layouts/home.html'}}

{{block 'title'}}{{'多人博客 - 首页'}}{{/block}}

{{block 'body'}}
<div style="margin: 90px 0px;">
  <section class="container">
    <div class="row">
      {{include '../_partials/settings-nav.html'}}
      <div class="col-md-5">
        <div class="subhead">
          <h2>修改密码</h2>
        </div>
        <hr>
        <form id="password_form">
          <div class="form-group">
            <label for="">当前密码</label>
            <input type="password" class="form-control" id="" placeholder="" name="opassword">
          </div>
          <div class="form-group">
            <label for="">新的密码</label>
            <input type="password" class="form-control" id="" placeholder="" name="npassword">
          </div>
          <div class="form-group">
            <label for="">确认密码</label>
            <input type="password" class="form-control" id="" placeholder="" name="cpassword">
          </div>
          <button type="submit" class="btn btn-success">保存</button>
        </form>
        <div class="subhead">
          <h2>注销账号</h2>
          <hr>
        </div>
        <div>
          <p>一旦注销，不可找回，请谨慎操作</p>
          <button  class="btn btn-danger" id="logoff">注销</button>
        </div>
        <hr>
      </div>
  
    </div>
  </section>
</div>
{{/block}}
{{block 'script'}}
  <script>
    $('#password_form').on('submit', function(e) {
      e.preventDefault();
      var formData = $(this).serialize();
      $.ajax({
        url: '/settings/admin',
        type: 'post',
        dataType: 'json',
        data: formData,
        async: true,
        success: function(data) {
          console.log('提交成功');
          console.log(data);
          var err_code = data.err_code;
          if(err_code === 0) {
            window.alert('密码修改成功')
            window.location.href = '/'
          }else if(err_code === 1) {
            window.alert('旧密码错误')
            window.location.reload()
          }else if(err_code === 2) {
            window.alert('不能和旧密码一致')
            window.location.reload()
          }else if(err_code === -1) {
            window.alert('新密码不一致')
            window.location.reload()
          }else {
            alert('服务器正忙，请稍后再试')
          }
          
        }
      })
    })
    $('#logoff').click(function(e) {
      var confirm = window.confirm('确定删除账号！')
      console.log(confirm);
      var confirmData = {confirm: confirm}
      $.ajax({
        url: '/logoff',
        data: confirmData,
        type: 'get',
        dataType: 'json',
        success: function(data) {
          var session = data
          var err_code = data.err_code;
          if(err_code === 0) {
            window.location.href = '/'
          }else {
            window.alert('已取消删除账号')
            window.location.reload();
          }
        }
      })
    })
  </script>
{{/block}}